package ex.com.jm.food.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import ex.com.jm.food.entity.User;
import ex.com.jm.food.service.UserService;
import ex.com.jm.food.utils.DateUtils;
import ex.com.jm.food.utils.JsonUtils;
import ex.com.jm.food.utils.MD5Utils;

/**
 * 
 * @author zhaojw
 *
 */
public class UserAction extends ActionSupport {

	
	private static final long serialVersionUID = 1L;

	private UserService userService;
	private User user;
	private String userId;
	private String userEmail;
	private String userPwd;
	private String userName;
	private String userPhone;
	private Integer status;
	private String address;
	private String errorInfo;
	private String province;
	private String citys;
	private String custom;
	private String countys;
	private String streets;
	private String nameError;
	private String emailError;
	private String phoneError;
	private String pwdError;
	private String realNameError;
	private String userImage;
	private String password;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public String getUserId() {
		return userId;
	}
	public void setUserId(String userId) {
		this.userId = userId;
	}
	public String getUserEmail() {
		return userEmail;
	}
	public void setUserEmail(String userEmail) {
		this.userEmail = userEmail;
	}
	public String getUserPwd() {
		return userPwd;
	}
	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getUserPhone() {
		return userPhone;
	}
	public void setUserPhone(String userPhone) {
		this.userPhone = userPhone;
	}
	public Integer getStatus() {
		return status;
	}
	public void setStatus(Integer status) {
		this.status = status;
	}
	
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	public String getErrorInfo() {
		return errorInfo;
	}
	public void setErrorInfo(String errorInfo) {
		this.errorInfo = errorInfo;
	}
	public String getProvince() {
		return province;
	}
	public void setProvince(String province) {
		this.province = province;
	}
	public String getCitys() {
		return citys;
	}
	public void setCitys(String citys) {
		this.citys = citys;
	}
	public String getCountys() {
		return countys;
	}
	public void setCountys(String countys) {
		this.countys = countys;
	}
	public String getStreets() {
		return streets;
	}
	public void setStreets(String streets) {
		this.streets = streets;
	}
	public String getNameError() {
		return nameError;
	}
	public void setNameError(String nameError) {
		this.nameError = nameError;
	}
	public String getEmailError() {
		return emailError;
	}
	public void setEmailError(String emailError) {
		this.emailError = emailError;
	}
	public String getPhoneError() {
		return phoneError;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public void setPhoneError(String phoneError) {
		this.phoneError = phoneError;
	}
	public String getPwdError() {
		return pwdError;
	}
	public void setPwdError(String pwdError) {
		this.pwdError = pwdError;
	}
	public String getRealNameError() {
		return realNameError;
	}
	public void setRealNameError(String realNameError) {
		this.realNameError = realNameError;
	}
	public String getCustom() {
		return custom;
	}
	public void setCustom(String custom) {
		this.custom = custom;
	}
	public String getUserImage() {
		return userImage;
	}
	public void setUserImage(String userImage) {
		this.userImage = userImage;
	}
	/**
	 * 登录页面
	 * @return
	 */
	public String login(){
		return "userLogin";
	}
	/**
	 * 登录
	 * @return 主页
	 */
	public String userLogin(){
		User userLogin=this.getUserService().userLogin(userEmail, MD5Utils.GetMD5Code(userPwd));
		if(userLogin==null){
			addFieldError("errorInfo", "用户名或者密码错误!");
			return "fail";
		}
		System.out.println(userLogin.getRealName());
		ActionContext.getContext().getSession().put("user", userLogin);
		custom=userLogin.getUserAddress().split(";")[4];
		ActionContext.getContext().getSession().put("custom", custom);
		return "success";
	}
	
	/**
	 * 用户注册页面
	 * @return
	 */
	public String editUser(){
		return "edit";
		
	}
	
	/**
	 * 用户注册
	 * @return 登录页面
	 */
	public String saveUser(){
		if (user.getUserName()==null||user.getUserName().equals("")) {
			addFieldError("nameError", "昵称不能为空");
			return "edit";
		}
		if(user.getUserEmail()==null||user.getUserEmail().equals("")){
			addFieldError("emailError", "邮箱不能为空");
			return "edit";
		}
		if(user.getUserPhone()==null||user.getUserPhone().equals("")){
			addFieldError("phoneError", "联系电话不能为空");
			return "edit";
		}
		if(user.getUserPwd()==null||user.getUserPwd().equals("")){
			addFieldError("pwdError", "密码不能为空");
			return "edit";
		}
		if(user.getRealName()==null||user.getRealName().equals("")){
			addFieldError("realNameError", "真实姓名不能为空");
			return "edit";
		}
		User checkName=this.getUserService().checkName(user.getUserName());
		if (checkName!=null) {
			addFieldError("nameError", "昵称已被注册");
			return "edit";
		}
		User checkEmail=this.getUserService().checkEmail(user.getUserEmail());
		if (checkEmail!=null) {
			addFieldError("emailError", "邮箱已被注册");
			return "edit";
		}
		User checkPhone=this.getUserService().checkPhone(user.getUserPhone());
		if(checkPhone!=null){
			addFieldError("phoneError", "联系电话已被注册");
			return "edit";
		}
		String userAddrsss=province+";"+citys+";"+countys+";"+streets+";"+custom;
		user.setUserAddress(userAddrsss);
		user.setUserPoints(0);
		user.setCreated(DateUtils.getCurrentDate());
		user.setStatus(0);
		user.setType(0);
		user.setUserPwd(MD5Utils.GetMD5Code(user.getUserPwd()));
		this.getUserService().saveUser(user);
		return "login";
	}
	/**
	 * 验证昵称
	 */
	public void checkName(){
		User checkName=this.getUserService().checkName(userName);
		String msg="";
		if(checkName!=null){
			msg="该昵称已被注册";
		}else{
			msg="该昵称可用";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	/**
	 * 验证邮箱
	 */
	public void checkEmail(){
		User checkEmail=this.getUserService().checkEmail(userEmail);
		String msg="";
		if(checkEmail!=null){
			msg="该邮箱已被注册";
		}else{
			msg="该邮箱可用";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	/**
	 * 验证电话
	 */
	public void checkPhone(){
		User checkPhone=this.getUserService().checkPhone(userPhone);
		String msg="";
		if(checkPhone!=null){
			msg="1";
		}else{
			msg="0";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public void checkPwd(){
		User checkPwd=this.getUserService().userLogin(userEmail, MD5Utils.GetMD5Code(userPwd));
		String msg="";
		if(checkPwd!=null){
			msg="1";
		}else{
			msg="0";
		}
		JSONArray jo=JsonUtils.buildMessage(msg);
		HttpServletResponse response = ServletActionContext.getResponse();  
		response.setContentType("text/plain;charset=utf-8");
		PrintWriter out=null;
		try {
			out = response.getWriter();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jo);
		out.flush();
		out.close();
	}
	
	public String Info(){
		return "userInfos";
	}
	/**
	 * 个人中心
	 * @return
	 */
	public String userInfo(){
		if(userId!=null){
			user=this.getUserService().editUser(userId);
		}
		System.out.println(user.getUserEmail());
		return "userInfo";
	}
	 /**
	  * 用户地址
	  * @return
	  */
	public String addressInfo(){
		if(userId!=null){
			user=this.getUserService().editUser(userId);
		}
		return "addressInfo";
		
	}
   /**
    * 更新地址
    * @return
    */
	public String updateAddess(){
		String userAddrsss=province+";"+citys+";"+countys+";"+streets+";"+custom;
		this.getUserService().updateAddress(userAddrsss, userId);
		return "info";
	}
	
	/**
	 * 更改密码
	 * @return
	 */
	public String pwdInfo(){
		if(userId!=null){
			user=this.getUserService().editUser(userId);
		}
		return "pwdInfo";
	}
	/**
	 * 更新密码
	 * @return
	 */
	public String updatePwd(){
		User checkPwd=this.getUserService().userLogin(userEmail, MD5Utils.GetMD5Code(password));
		if(checkPwd!=null){
			this.getUserService().updatePwd(MD5Utils.GetMD5Code(userPwd), userId);
			return "login";
		}
		return "pwdInfo";
		
		
	}
	
	public String phoneInfo(){
		if(userId!=null){
			user=this.getUserService().editUser(userId);
		}
		return "phoneInfo";
	}
	
	/**
	 * 更新联系方式
	 * @return
	 */
	public String updatePhone(){
		this.getUserService().updatePhone(userPhone, userId);
		return "info";
	}
	/**'
	 * 申请会员
	 * @return
	 */
	public String updateStatus(){
		this.getUserService().updateStatus(status, userId);
		return "info";
	}
	
	public String editUserImage(){
		if(userId!=null){
			user=this.getUserService().editUser(userId);
		}
		return "userImageInfo";
	}
	
	/**
	 * 上传头像
	 * @return
	 */
	public String updateUserImage(){
		this.getUserService().updateUserImage(userImage, userId);
		return "info";
	}
	
	@SuppressWarnings("rawtypes")
	public String logout(){
		ActionContext ac = ActionContext.getContext();
		Map session = ac.getSession();
		session.remove("user");
		return "userLogin";
	}
}
